<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 13:01
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\View;
use think\facade\Filesystem;

class Insurance extends AdminBase
{
    public function index()
    {
        $agency = \app\common\model\Agency::getAgency();
        $count = Db::name('insurance')->count();
        return View::fetch('index',['agency'=>$agency,'ecount'=>$count]);
    }

    //分类管理
    public function insurance_json()
    {

        $start_time = input('start_time',strtotime('1970-10-01'));
        $end_time = input('end_time',time());
        $code = input('code');
        $bname = input('bname');
        $cname = input('cname');
        $billCode = input('billCode');

        if(input('start_time') != ''){
            $start_time = strtotime($start_time);
        }else{
            $start_time = strtotime('1970-10-01');
        }

        if(input('end_time') != ''){
            $end_time = strtotime($end_time);
        }else{
            $end_time = time();
        }


        $map = [['create_time','between',[$start_time,$end_time]]];

        if(!empty( $code)){
            $eid = Db::name('employee')->where('code','like',$code)->value('id');
            array_push($map,['eid','=',$eid]);
        }

        if(!empty($cname)){
            array_push($map,['cname','like','%'.$cname.'%']);
        }

        if(!empty($bname)){
            array_push($map,['bname','like','%'.$bname.'%']);
        }

        if(!empty($billCode)) {
            array_push($map, ['billCode', 'like', '%' . $billCode . '%']);
        }


        $list = Db::name('insurance')
            ->where($map)
            ->order('create_time')
            ->paginate(10)->each(function($item, $key){
                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
                $item['commission_num'] = $item['acommission'] - $item['commission'];
                return $item;
            });

        $this->result($list);
    }

    //分类删除
    public function insurance_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if(session('admin_auth.admin_id') != 1) $this->error('无权进行此操作');

            Db::name('insurance')->where('id','in',$param['id'])->delete();
            insert_admin_log('删除了保险公司');
            $this->success('删除成功');
        }
    }

    //导出
    //导出留言
    public function expinsurance(){
        $start_time = input('start_time',strtotime('1970-10-01'));
        $end_time = input('end_time',time());
        $code = input('code');
        $bname = input('bname');
        $cname = input('cname');
        $billCode = input('billCode');

        if(input('start_time') != ''){
            $start_time = strtotime($start_time);
        }else{
            $start_time = strtotime('1970-10-01');
        }

        if(input('end_time') != ''){
            $end_time = strtotime($end_time);
        }else{
            $end_time = time();
        }


        $map = [['create_time','between',[$start_time,$end_time]]];

        if(!empty( $code)){
            $eid = Db::name('employee')->where('code','like',$code)->value('id');
            array_push($map,['eid','=',$eid]);
        }

        if(!empty($cname)){
            array_push($map,['cname','like','%'.$cname.'%']);
        }

        if(!empty($bname)){
            array_push($map,['bname','like','%'.$bname.'%']);
        }

        if(!empty($billCode)) {
            array_push($map, ['billCode', 'like', '%' . $billCode . '%']);
        }


        $data = Db::name('insurance')
            ->where($map)
            ->order('create_time')
            ->select()->toArray();

        foreach($data as $k => $v){
            $data[$k]['create_time'] =  date('Y-m-d H:i:s',$v['create_time']);
            $data[$k]['commission_num'] =  $v['acommission'] - $v['commission'];
        }

        // 引入类库
        $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();;

        // 设置当前sheet
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->setTitle('业务员信息');
        $list = ['序号','机构名称','保险公司','出单方名称','机构佣金','业务员名称','保单编号','投保人姓名','车牌号','保单金额','佣金金额','创建日期','中收'];
        $strarr = ['A','B','C','D','E','F','G','H','I','J','K', 'L','M','N',"O",'P','Q','R','S','T','U','V',
            'W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP',
            'AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ',
            'BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ','CA','CB','CC','CD','CE','CF','CG'];

        foreach($list as $k => $v){
            $obj->getActiveSheet()->setCellValue($strarr[$k]. 1,$v);
        }

        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('G')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('I')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('J')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('K')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('L')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('M')->setWidth(20);
        $count = 2;
        foreach($data as $k => $v){
            $obj->getActiveSheet()->setCellValue('A'.$count,$v['billCode']);
            $obj->getActiveSheet()->setCellValue('B'.$count,$v['aname']);
            $obj->getActiveSheet()->setCellValue('C'.$count,$v['cname']);
            $obj->getActiveSheet()->setCellValue('D'.$count,$v['bname']);
            $obj->getActiveSheet()->setCellValue('E'.$count,$v['acommission']);
            $obj->getActiveSheet()->setCellValue('F'.$count,$v['ename']);
            $obj->getActiveSheet()->setCellValue('G'.$count,$v['insureCode']);
            $obj->getActiveSheet()->setCellValue('H'.$count,$v['ownername']);
            $obj->getActiveSheet()->setCellValue('I'.$count,$v['plate']);
            $obj->getActiveSheet()->setCellValue('J'.$count,$v['price']);
            $obj->getActiveSheet()->setCellValue('K'.$count,$v['commission']);
            $obj->getActiveSheet()->setCellValue('L'.$count,$v['create_time']);
            $obj->getActiveSheet()->setCellValue('M'.$count,$v['commission_num']);
            $obj->getActiveSheet()->getRowDimension($count)->setRowHeight(30);
            $count++;
        }
        $obj->getActiveSheet()->getStyle('A1:Q2000')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
        $obj->getActiveSheet()->getStyle('A1:Q2000')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);

        $fileName =  '保单信息';
        // 导出
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8');
        header('Content-Disposition: attachment;filename="' . $fileName.'.xlsx');
        header('Cache-Control: max-age=1');
        $objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($obj, 'Xlsx');
        $objWriter->save('php://output');
        exit;
    }
}